<template>
  <div>
    <el-dialog
      :visible="isshow"
      :title="{ add: '新增角色', edit: '修改角色' }[mode]"
      @close="closeEvent"
    >
      <!-- ref提供组件实例化对象 -->
      <!-- :model绑定表单数据 -->
      <!-- rules绑定表单验证 -->
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysRolePost, sysRolePut } from '@/api/setting'
export default {
  model: {
    prop: 'isshow',
    event: 'input'
  },
  // props: {
  //   isshow: {
  //     type: Boolean,
  //     defalut: false
  //   }
  // },
  data() {
    return {
      isshow: false,
      mode: 'add',
      form: {
        name: '',
        description: ''
      },
      // 定义表单验证
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    closeEvent() {
      this.isshow = false
    },
    submit() {
      // 验证设置表单
      this.$refs.form.validate(async(result) => {
        // 判断是否成功
        if (result) {
          // 调用接口
          if (this.mode === 'add') {
            const res = await sysRolePost(this.form)
            this.$message.success('添加成功')
            console.log(res, '设置添加成员成功')
          } else if (this.mode === 'edit') {
            await sysRolePut(this.form)
            this.$message.success('编辑成功')
          }
          // 关闭弹框
          this.closeEvent()
          // 重新渲染数据
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style></style>
